<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/7
 * Time: 14:03
 */
$dispatcher = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $r){
	$r->addRoute('GET','/','App\Demo\Controllers\IndexController?doo=fafd@jjjf');
	$r->addRoute('GET','/demo/{o:\d+}/{title:[a-zA-Z-_]+}','App\Demo\Controllers\HomeController@home');
	$r->addRoute('POST','/doo','App\Demo\Controllers\HomeController@doo');
});

// Fetch method and URI from somewhere
$httpMethod = $_SERVER['REQUEST_METHOD'];
$uri = $_SERVER['REQUEST_URI'];

//echo $uri.'<br/>';
//echo parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH).'<br/>';

// Strip query string (?foo=bar) and decode URI
if (false !== $pos = strpos($uri, '?')) {
	$uri = substr($uri, 0, $pos);
}
$uri = rawurldecode($uri);

//echo $uri.'<br/>';

$routeInfo = $dispatcher->dispatch($httpMethod, $uri);
switch ($routeInfo[0]) {
	case FastRoute\Dispatcher::NOT_FOUND:
		// ... 404 Not Found
		echo "404 Not Found!";
		break;
	case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
		$allowedMethods = $routeInfo[1];
		// ... 405 Method Not Allowed
		echo "405 Method Not Allowed";
		break;
	case FastRoute\Dispatcher::FOUND:
		$handler = $routeInfo[1];
		$vars = $routeInfo[2];
		// ... call $handler with $vars
		//var_dump ($handler.'<br/>');
		//var_dump ($vars);
		//$vars = implode(',',$vars);
		//echo '<br />';
		//echo $vars.'<br />';
		$handler = explode('@',$handler);
		//var_dump($handler);
		//echo '<br />'.$handler[0].'<br />';
		//echo $handler[1].'<br />';
		//call_user_func_array(array($handler[0],$handler[1]),$vars);
		$controller = new $handler[0]();

		if (!method_exists($controller,$handler[1]))
		{
			echo "controller and method not found!";
		} else {
			call_user_func_array(array($controller,$handler[1]),$vars);
			//$controller->{$handler[1]}($vars);
		}

		break;
}